﻿using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LevelUp : MonoBehaviour {

	public int level = 1;
	public int maxLevel;
	public GameObject[] LevelObject;
	public Text centerText;

	// Use this for initialization
	void Start () 
	{
		centerText = GameObject.Find ("LevelText").GetComponent<Text>();
		if (!PlayerPrefs.HasKey ("Level")) {
			level = 1;
			PlayerPrefs.SetInt ("Level", level);
		} 
		else 
		{
			level = PlayerPrefs.GetInt ("Level");
		}
		if (!PlayerPrefs.HasKey ("MaxLevel")) {
			maxLevel = level;
			PlayerPrefs.SetInt ("MaxLevel", maxLevel);
		} else {
			maxLevel = PlayerPrefs.GetInt ("MaxLevel");
		}
		//CheckLevel ();
		centerText.text = level.ToString ();
		GameObject levelPrefab = Instantiate (LevelObject[(level-1)%3]) as GameObject;
		levelPrefab.transform.parent = GameObject.Find ("Center").transform;
		this.SendMessage ("Reset",level);
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void OnWin()
	{
		level++;
		//CheckLevel ();
		PlayerPrefs.SetInt ("Level",level);
		maxLevel = level > maxLevel ? level : maxLevel;
		PlayerPrefs.SetInt ("MaxLevel",maxLevel);
		SceneManager.LoadScene (2);
	}

	public void CheckLevel()
	{
		if (level>LevelObject.Length) {
			level = 1;
			PlayerPrefs.SetInt ("Level",level);
			SceneManager.LoadScene (2);
			return;
		}
	}
}
